<?php
require_once("Accountant.class.php");
require_once("exceptions/AppExceptions.class.php");

class SpecialMainStateAccountant extends Accountant {
	
	private $certificationNumber = array();
	
	private $sampleBatch = array();

	private $materialCode = array();

	private $sequenceNumber = array();
	 
	private $manufactory = array();
	
	private $authenticationMark = array();
	
	private $steelNumber =  array();
	
	private $size1 = array();
	
	private $size2 = array();
	
	private $size3 = array();
	
	private $main3Count = array();
	
	private $main3SumCount = array();
	
	private $main3AllocatedCount = array();

	private $mainAllocatedCount = array();
	
	private $mainCount = array();
	
	private $mainSumCount = array();
	
	private $mainCertificationCount = array();
	
	private $unitWeight = array();
	
	private $orderNumber = array();
	
	private $orderSubitemNumber = array();
	
	private $unitPrice = array();
	
	private $purchaseNumber = array();
	
	private $remark = array();
	
	private $certificationCodification = array();
	
	private	$materialNumber = array();
	
	private	$shipmentDate = array();
	
	private	$month = array();
	
	private	$vehicleNumber = array();
	
	private	$consignee = array();
	/**
	 * 构造函数，根据文件名称初始化sql语句。
	 *
	 * @param string $filename
	 */
	public function __construct($filename){
		$query = "select uploadTime from little_main3 where filename = '$filename' limit 1";
		DB::query($query);
		$row = DB::getResult()->fetch_assoc();
		$uploadTime = $row['uploadTime'];
		$this->sql = "select *,sum(count) as main3Count from little_main3 where filename = '$filename'
					group by orderNumber,orderSubitemNumber
					order by materialCode, size1, size2, size3";
	}
	/**
	 * @return the $main3AllocatedCount
	 */
	public function getMain3AllocatedCount() {
		return $this->main3AllocatedCount;
	}

	

	/**
	 * 执行查询命令，并整理查询的结果
	 *
	 */
	public function execute(){
		$this->result = DB::query($this->sql);
		if($this->result == false){
			return;
		}
		while($row = $this->result->fetch_assoc()){
			$main3Sql = "select * from
					(
					select *,sum(`count`) as main3Count from little_main3
					where orderNumber = '{$row['orderNumber']}' and orderSubitemNumber = '{$row['orderSubitemNumber']}'
					group by materialCode
					order by main3Count
					)as main3Table
					 
					left outer join
					
					(
					select orderNumber, orderSubitemNumber,sum(`count`) as main3SumCount from little_main3
					group by orderNumber, orderSubitemNumber
					)as main3SumTable
					using (orderNumber, orderSubitemNumber)
					";
			$main3Result = DB::query($main3Sql);
			$materialCode = array();
			$main3Count = array();
			$main3SumCount = array();
			$sequenceNumber = array();
	 		$manufactory = array();
			$authenticationMark = array();
			$steelNumber =  array();
			$size1 = array();
	 		$size2 = array();
			$size3 = array();
			$unitWeight = array();
			$orderNumber = array();
			$orderSubitemNumber = array();
			$unitPrice = array();
			$purchaseNumber = array();
			$remark = array();
			while(($main3Row = $main3Result->fetch_assoc())!=null){
				array_push($materialCode,$main3Row['materialCode']);
				array_push($main3Count, $main3Row['main3Count']);
				array_push($main3SumCount, $main3Row['main3SumCount']);
				array_push($sequenceNumber, $main3Row['sequenceNumber']);
				array_push($authenticationMark, $main3Row['authenticationMark']);
				array_push($manufactory, $main3Row['manufactory']);
				array_push($steelNumber, $main3Row['steelNumber']);
				array_push($size1, $main3Row['size1']);
				array_push($size2, $main3Row['size2']);
				array_push($size3, $main3Row['size3']);
				array_push($unitWeight, $main3Row['unitWeight']);
				array_push($orderNumber, $main3Row['orderNumber']);
				array_push($orderSubitemNumber, $main3Row['orderSubitemNumber']);
				array_push($unitPrice, $main3Row['unitPrice']);
				array_push($purchaseNumber, $main3Row['purchaseNumber']);
				array_push($remark, $main3Row['remark']);
			}
			
			$mainSql = "select * from
					(
					select *,sum(`count`) as mainCount from little_main
					where orderNumber = '{$row['orderNumber']}' and orderSubitemNumber = '{$row['orderSubitemNumber']}'
					group by certificationNumber, sampleBatch
					order by mainCount
					)as mainTable
					
					left outer join
					
					(
					select orderNumber, orderSubitemNumber,sum(`count`) as mainSumCount from little_main
					group by orderNumber, orderSubitemNumber
					)as mainSumTable
					using (orderNumber, orderSubitemNumber)
					
					left outer join
					
					(
					select certificationNumber,orderNumber, orderSubitemNumber, sum(`count`) as mainCertificationCount
					from little_main
					group by certificationNumber
					)as mainCertificationTable
					using (certificationNumber, orderNumber, orderSubitemNumber)
					";
			$mainResult = DB::query($mainSql);
			$certificationNumber = array();
			$sampleBatch = array();
			$certificationCodification = array();
			$materialNumber = array();
			$shipmentDate = array();
			$month = array();
			$vehicleNumber = array();
			$consignee = array();
			$mainCount = array();
			$mainSumCount = array();
			$mainCertificationCount = array();
			while(($mainRow = $mainResult->fetch_assoc())!=null){
				array_push($certificationNumber, $mainRow['certificationNumber']);
				array_push($sampleBatch, $mainRow['sampleBatch']);
				array_push($certificationCodification, $mainRow['certificationCodification']);
				array_push($materialNumber, $mainRow['materialNumber']);
				array_push($shipmentDate, $mainRow['shipmentDate']);
				array_push($month, $mainRow['month']);
				array_push($vehicleNumber, $mainRow['vehicleNumber']);
				array_push($consignee, $mainRow['consignee']);
				array_push($mainCount, $mainRow['mainCount']);
				array_push($mainSumCount,$mainRow['mainSumCount']);
				array_push($mainCertificationCount,$mainRow['mainCertificationCount']);
			}

			//是否需要对mainCountTemp重新赋值
			$resetMainCountTemp = true;
			
			//分配原则算法
			for ($main3Key = count($main3Count)-1;$main3Key >=0 ;$main3Key--){
				$main3CountTemp = $main3Count[$main3Key];
				$main3AllocatedCountTemp = 0;
				$loopCount = 0;
				for($mainKey = count($mainCount)-1;$mainKey >=0 ;$mainKey--,$loopCount++) {
					if($resetMainCountTemp)
						$mainCountTemp = $mainCount[$mainKey];
					array_push($this->mainSumCount, $mainSumCount[$mainKey]);
					array_push($this->mainCount,$mainCount[$mainKey]);
					array_push($this->mainCertificationCount, $mainCertificationCount[$mainKey]);
					array_push($this->certificationNumber,$certificationNumber[$mainKey]);
					array_push($this->sampleBatch,$sampleBatch[$mainKey]);
					array_push($this->certificationCodification,$certificationCodification[$mainKey]);
					array_push($this->materialNumber,$materialNumber[$mainKey]);
					array_push($this->shipmentDate,$shipmentDate[$mainKey]);
					array_push($this->month,$month[$mainKey]);
					array_push($this->vehicleNumber,$vehicleNumber[$mainKey]);
					array_push($this->consignee,$consignee[$mainKey]);
					array_push($this->materialCode,$materialCode[$main3Key]);
					array_push($this->main3SumCount,$main3SumCount[$main3Key]);
					array_push($this->main3Count,$main3Count[$main3Key]);
					array_push($this->sequenceNumber,$sequenceNumber[$main3Key]);
					array_push($this->manufactory,$manufactory[$main3Key]);
					array_push($this->authenticationMark,$authenticationMark[$main3Key]);
					array_push($this->steelNumber,$steelNumber[$main3Key]);
					array_push($this->size1,$size1[$main3Key]);
					array_push($this->size2,$size2[$main3Key]);
					array_push($this->size3,$size3[$main3Key]);
					array_push($this->unitWeight,$unitWeight[$main3Key]);
					array_push($this->orderNumber,$orderNumber[$main3Key]);
					array_push($this->orderSubitemNumber,$orderSubitemNumber[$main3Key]);
					array_push($this->unitPrice,$unitPrice[$main3Key]);
					array_push($this->purchaseNumber,$purchaseNumber[$main3Key]);
					array_push($this->remark,$remark[$main3Key]);
					if($main3CountTemp<$mainCountTemp){
						array_push($this->mainAllocatedCount,$main3CountTemp);
						array_push($this->main3AllocatedCount,$main3CountTemp);
						$mainCountTemp = $mainCountTemp - $main3CountTemp;
						array_pop($main3Count);
						$resetMainCountTemp = false;
						continue 2;
					}else if($main3CountTemp>$mainCountTemp){
						array_push($this->mainAllocatedCount,$mainCountTemp);
						array_push($this->main3AllocatedCount,$mainCountTemp);
						$main3CountTemp = $main3CountTemp - $mainCountTemp;
						if($loopCount>0)
							$main3AllocatedCountTemp = $main3AllocatedCountTemp + $mainCountTemp;
						else 
							$main3AllocatedCountTemp = $mainCountTemp;
						array_pop($mainCount);
						$resetMainCountTemp = true;
						continue;
					}else if($main3CountTemp==$mainCountTemp){
						array_push($this->mainAllocatedCount,$mainCountTemp);
						array_push($this->main3AllocatedCount,$mainCountTemp);
						array_pop($main3Count);
						array_pop($mainCount);
						$resetMainCountTemp = true;
						continue 2;
					}
				}
				if(empty($mainCount)){
					array_push($this->mainAllocatedCount, 0);
					array_push($this->mainSumCount, 0);
					array_push($this->mainCount,0);
					array_push($this->mainCertificationCount,0);
					array_push($this->certificationNumber,'');
					array_push($this->sampleBatch,'');
					array_push($this->certificationCodification,'');
					array_push($this->materialNumber,'');
					array_push($this->shipmentDate,'');
					array_push($this->month,'');
					array_push($this->vehicleNumber,'');
					array_push($this->consignee,'');
					array_push($this->materialCode,$materialCode[$main3Key]);
					array_push($this->main3SumCount,$main3SumCount[$main3Key]);
					array_push($this->main3Count,$main3Count[$main3Key]);
					array_push($this->main3AllocatedCount,$main3Count[$main3Key] - $main3AllocatedCountTemp);
					array_push($this->sequenceNumber,$sequenceNumber[$main3Key]);
					array_push($this->manufactory,$manufactory[$main3Key]);
					array_push($this->authenticationMark,$authenticationMark[$main3Key]);
					array_push($this->steelNumber,$steelNumber[$main3Key]);
					array_push($this->size1,$size1[$main3Key]);
					array_push($this->size2,$size2[$main3Key]);
					array_push($this->size3,$size3[$main3Key]);
					array_push($this->unitWeight,$unitWeight[$main3Key]);
					array_push($this->orderNumber,$orderNumber[$main3Key]);
					array_push($this->orderSubitemNumber,$orderSubitemNumber[$main3Key]);
					array_push($this->unitPrice,$unitPrice[$main3Key]);
					array_push($this->purchaseNumber,$purchaseNumber[$main3Key]);
					array_push($this->remark,$remark[$main3Key]);
				}
			}
		}
	}
	

	/**
	 * @return the $mainCount
	 */
	public function getMainCount() {
		return $this->mainCount;
	}

	/**
	 * @return the $mainAllocatedCount
	 */
	public function getMainAllocatedCount() {
		return $this->mainAllocatedCount;
	}
	
	/**
	 * @return the $certificationNumber
	 */
	public function getCertificationNumber() {
		return $this->certificationNumber;
	}

	
	/**
	 * @return the $sampleBatch
	 */
	public function getSampleBatch() {
		return $this->sampleBatch;
	}

	
	/**
	 * @return unknown
	 */
	public function getAuthenticationMark() {
		return $this->authenticationMark;
	}
	
	/**
	 * @return unknown
	 */
	public function getMaterialCode() {
		return $this->materialCode;
	}
	
	/**
	 * @return the $mainSumCount
	 */
	public function getMainSumCount() {
		return $this->mainSumCount;
	}

	/**
	 * @return the $main3SumCount
	 */
	public function getMain3SumCount() {
		return $this->main3SumCount;
	}

	/**
	 * @return the $main3Count
	 */
	public function getMain3Count() {
		return $this->main3Count;
	}

	
	
	/**
	 * @return int
	 */
	public function getFileId() {
		return $this->fileId;
	}
	
	/**
	 * @return unknown
	 */
	public function getManufactory() {
		return $this->manufactory;
	}

	/**
	 * @return unknown
	 */
	public function getOrderNumber() {
		return $this->orderNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getOrderSubitemNumber() {
		return $this->orderSubitemNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getPurchaseNumber() {
		return $this->purchaseNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getRemark() {
		return $this->remark;
	}
	
	/**
	 * @return unknown
	 */
	public function getSequenceNumber() {
		return $this->sequenceNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getSize1() {
		return $this->size1;
	}
	
	/**
	 * @return unknown
	 */
	public function getSize2() {
		return $this->size2;
	}
	
	/**
	 * @return unknown
	 */
	public function getSize3() {
		return $this->size3;
	}
	
	/**
	 * @return unknown
	 */
	public function getSteelNumber() {
		return $this->steelNumber;
	}
	
	/**
	 * @return unknown
	 */
	public function getUnitPrice() {
		return $this->unitPrice;
	}
	
	/**
	 * @return unknown
	 */
	public function getUnitWeight() {
		return $this->unitWeight;
	}
	
	/**
	 * @return the $consinee
	 */
	public function getConsignee() {
		return $this->consignee;
	}

	/**
	 * @return the $vehicleNumber
	 */
	public function getVehicleNumber() {
		return $this->vehicleNumber;
	}

	/**
	 * @return the $month
	 */
	public function getMonth() {
		return $this->month;
	}

	/**
	 * @return the $shipmentDate
	 */
	public function getShipmentDate() {
		return $this->shipmentDate;
	}

	/**
	 * @return the $materialNumber
	 */
	public function getMaterialNumber() {
		return $this->materialNumber;
	}

	/**
	 * @return the $certificationCodification
	 */
	public function getCertificationCodification() {
		return $this->certificationCodification;
	}

	/**
	 * @return the $mainCertificationCount
	 */
	public function getMainCertificationCount() {
		return $this->mainCertificationCount;
	}
	
}
?>